from django import http
from django.contrib.auth.mixins import LoginRequiredMixin

from response_code import RETCODE, err_msg


class LoginRequiredJsonMixin(LoginRequiredMixin):
    """重写LoginRequiredMixin，返回json数据"""

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return http.JsonResponse({'code': RETCODE.SESSIONERR, 'errmsg': err_msg[RETCODE.SESSIONERR]})
        return super().dispatch(request, *args, **kwargs)
